package pluginMain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;

public class DialogManager implements Map<Dialog, ListItemEventListener> {
	private List<Dialog> dialogsHolder = new ArrayList<Dialog>();
	private List<ListItemEventListener> eventsHolder = new ArrayList<ListItemEventListener>();

	public int registerDialog(Dialog dialog, ListItemEventListener listener) {
		dialogsHolder.add(dialog);
		eventsHolder.add(listener);
		return dialogsHolder.size();
	}

	public Dialog getDialog(Inventory inventory) {
		for (Dialog d : dialogsHolder) {
			if (d.getInventory().equals(inventory))
				return d;
		}
		return null;
	}

	public Dialog getDialog(int index) {
		return dialogsHolder.get(index);
	}

	public void openDialog(int index, Player who) {
		dialogsHolder.get(index).openDialog(who);
	}

	public boolean dialogExist(Dialog dialog) {
		if (dialogsHolder.contains(dialog))
			return true;
		return false;
	}

	public boolean dialogExist(Inventory inventory) {
		for (Dialog d : dialogsHolder) {
			if (d.getInventory().equals(inventory))
				return true;
		}
		return false;
	}

	public ListItemEventListener getListener(Dialog dialog) {
		return eventsHolder.get(dialogsHolder.indexOf(dialog));
	}

	public ListItemEventListener getListener(int index) {
		return eventsHolder.get(index);
	}

	@Override
	public int size() {
		return dialogsHolder.size();
	}

	@Override
	public boolean isEmpty() {
		return dialogsHolder.isEmpty();
	}

	@Override
	public boolean containsKey(Object key) {
		return dialogsHolder.contains(key);
	}

	@Override
	public boolean containsValue(Object value) {
		return eventsHolder.contains(value);
	}

	@Override
	public ListItemEventListener get(Object key) {
		return eventsHolder.get(dialogsHolder.indexOf(key));
	}

	@Override
	public ListItemEventListener put(Dialog key, ListItemEventListener value) {
		dialogsHolder.add(key);
		eventsHolder.add(value);
		return value;
	}

	@Override
	public ListItemEventListener remove(Object key) {
		ListItemEventListener i = eventsHolder.get(dialogsHolder.indexOf(key));
		dialogsHolder.remove(key);
		eventsHolder.remove(i);
		return i;
	}

	@Override
	public void putAll(Map<? extends Dialog, ? extends ListItemEventListener> m) {
		dialogsHolder.addAll(m.keySet());
		eventsHolder.addAll(m.values());
	}

	@Override
	public void clear() {
		dialogsHolder.clear();
		eventsHolder.clear();

	}

	@SuppressWarnings("unchecked")
	@Override
	public Set<Dialog> keySet() {
		return (Set<Dialog>) dialogsHolder;
	}

	@Override
	public Collection<ListItemEventListener> values() {
		return eventsHolder;
	}

	@Override
	public Set<java.util.Map.Entry<Dialog, ListItemEventListener>> entrySet() {
		return null;
	}

}
